package code.google.antanno;

import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.MethodVisitor;

import java.util.*;

class CheckAnnotation implements ClassVisitor {
    private String target       = null;
    private boolean exists      = false;
    private List<String> value  = new ArrayList<String>();
                                
    private AnnotationVisitor av = new AnnotationVisitor() {
        public void visit(String name, Object value) {
            if (value != null) CheckAnnotation.this.value.add(String.valueOf(value));
        }

        public void visitEnum(String name, String desc, String value) {
            CheckAnnotation.this.value.add(value);
        }

        public AnnotationVisitor visitAnnotation(String name, String desc) {
            return null;
        }

        public AnnotationVisitor visitArray(String name) {
            return this;
        }

        public void visitEnd() {
        }
    };
  
    public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
    }
  
    public void visitSource(String source, String debug) {
    }
  
    public void visitOuterClass(String owner, String name, String desc) {
    }
  
    public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
        if (target != null && target.equals(desc)) {
            exists = true;
            return av;
        } else {
            return null;
        }
    }
  
    public void visitAttribute(Attribute attr) {
    }
  
    public void visitInnerClass(String name, String outerName, String innerName, int access) {
    }
  
    public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
        return null;
    }
  
    public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
        return null;
    }
  
    public void visitEnd() {
    }

    void setTargetAnnotation(String target) {
        this.target = target;
    }
    
    boolean isExists() {
        return exists;
    }

    List<String> getValue() {
        return value;
    }
}
